<template>
  <div>
    <tableComponents
      :pagination="{currentPage:currentPage1,pageSize,total:list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getNewsSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="add"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.9%"
          :default-sort="{prop: 'sort',}"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input
                @change="sortNew(scope.$index,scope.row)"
                v-model.number="scope.row.sort"
                autocomplete="off"
                size="small"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="120">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column
            prop="picture"
            header-align="center"
            align="center"
            min-width="200px"
            label="图片"
          >
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="scope.row.piclink" style="width:500px;" />
                <img
                  slot="reference"
                  :src="scope.row.piclink"
                  :alt="scope.row.piclink"
                  style="width:100px"
                />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column prop="address" label="链接" min-width="170" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.links }}</template>
          </el-table-column>

          <el-table-column prop="address" label="地区" min-width="170" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.province+scope.row.city+scope.row.area }}</template>
          </el-table-column>

          <el-table-column prop="address" label="层级" min-width="170" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.types==0?'市':'区' }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否显示" min-width="125">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_show"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="已开启"
                inactive-text="已关闭"
                active-value="1"
                @change="getIs($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                type="primary"
                size="mini"
                @click="updateNews(scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="deleteNew( scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="编辑幻灯片"
      class="tl size_13"
      :append-to-body="true"
      :visible.sync="showSlideFrom"
      width="800px"
    >
      <supplier-slide-from v-if="showSlideFrom" @hideDio="getHideDio" :row="row" />
    </el-dialog>
  </div>
</template>
<script>
import supplierSlideFrom from "./supplier-slide/supplier-slide-from";
import { supplierSlideSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: supplierSlideSearchData,
      optionProps: {
        value: "id",
        label: "title",
        children: "z"
      },
      data: [],
      showSlideFrom: false,
      copy_form: {
        id: "",
        cate_id: Number
      },
      copy_show: false,
      loading: false,
      currentPage1: 1,
      list: {},
      pageSize: 10,
      multipleSelection: [],
      sort: "",
      searchAll: {},
      row: {}
    };
  },

  components: {

    supplierSlideFrom
  },
  methods: {
    getHideDio(bool) {
      this.showSlideFrom = false;
      if (bool) {
        this.getNewsAll();
      }
    },
    add() {
      this.showSlideFrom = true;
      this.row = {};
    },
    getNewsSearch(obj) {
      this.searchAll = obj;
      this.$router.push(this.$route.path + "?page=" + 1);
      this.$store.dispatch("getNewsList", obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      this.$api.getSupplier.getSupplierSlide(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          this.list = res.data.result;
          console.log(res);
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;

      this.getNewsAll(params);
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.$router.push(this.$route.path + "?page=" + val);
    },
    updateNews(row) {
      this.showSlideFrom = true;
      this.row = row;
    },
    deleteNew(id) {
      //删除
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getSupplier.delSupplierSlide(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll("", this.pageSize);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getTop(val, row) {
      var params = {};
      params.id = row.id;
      params.is_top = val;
      this.updata(params);
    },
    getIs(val, row) {
      var params = {};
      params = row;
      params.is_show = val;
      this.updata(params);
    },
    deleteLises() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getSupplier.delSupplierSlide(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getNewsAll("", this.pageSize);
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
              }
            });
          }
        });
      }
    },
    sortNew(index, row) {
      var params = {};
      params = row;
      params.sort = row.sort;
      this.updata(params);
    },
    updata(row) {
      var params = {};
      params = row;
      this.$api.getSupplier.editSupplierSlide(params).then(res => {
        if (res.data.status == 1 && res.data.code == 200) {
          //  this.$message.success('提交成功');
          if (params.sort) {
            var obj = {};
            obj.page = "";
            obj.page_size = this.pageSize;
            this.getNewsAll(this.searchAll);
          }
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    sorta(a, b) {
      return b - a;
    }
  },
  mounted() {},
  created() {
    var params = this.searchAll;
    params.page = this.$route.query.page || 1;
    params.page_size = this.pageSize;
    this.getNewsAll(params);
    this.currentPage1 = Number(this.$route.query.page) || 1;
  },
  computed: {},
  watch: {
    $route() {
      var params = this.searchAll;
      params.page = this.$route.query.page || 1;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
      this.currentPage1 = Number(this.$route.query.page) || 1;
    }
  }
};
</script>
<style lang="less" scoped>
</style>
